<?php

/**
 * @author      :   
 * @name        :   My_Plugin_Change_Skin
 * @version     :   1.0
 * @copyright   :   FPT Online
 * @todo        :   
 */
class My_Plugin_ChangeSkin extends Zend_Controller_Plugin_Abstract {

    /**
     * Called before Zend_Controller_Front enters its dispatch loop.
     * @param Zend_Controller_Request_Abstract $request
     */
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
        //hard code
        $strTheme = 'theme01';
        //Get module
        $strModule = $request->getModuleName();
        $strController = strtolower($request->getControllerName());
        $strAction = strtolower($request->getActionName());

        $strThemePath = SKIN_PATH . DS . $strTheme . DS . 'application';
        $fileView = $strThemePath . DS . 'modules' . DS . $strModule . DS . 'views' . DS . 'scripts' . DS . $strController . DS . $strAction . '.phtml';
        $fileLayout = $strThemePath . DS . 'layouts' . DS . $strModule . DS . 'layout.phtml';
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        if (null === $viewRenderer->view) {
            $viewRenderer->initView();
        }
        $view = $viewRenderer->view;
        if ($strModule == 'frontend') {
            if (file_exists($fileLayout)) {
                Zend_Layout::getMvcInstance()->setLayoutPath($strThemePath . DS . 'layouts' . DS . $strModule);
            }
            if (file_exists($fileView)) {
                $view->setScriptPath(
                        array(
                            APPLICATION_PATH . DS . 'modules' . DS . $strModule . DS . 'views' . DS . 'scripts',
                            $strThemePath . DS . 'modules' . DS . $strModule . DS . 'views' . DS . 'scripts',
                        )
                );
            }
            $view->addHelperPath('My/Helper/', 'My_Helper');
            //Cleanup
            unset($viewRenderer, $view);
            unset($instanceController, $strController, $strModule, $strAction, $strThemePath, $fileView, $fileLayout);
        }
    }

}